#include <iostream>

using namespace std;

// 自定义异常
struct IllegalException : public exception {
    const char *what() const noexcept override {
        return "C++ Exception";
    }
};

class Fraction {
public:
    // 无参构造函数
    Fraction();

    // 构造函数
    Fraction(int newNume, int newDeno);

    // 设置对象属性
    void setElem(int newNume, int newDeno);

    // 是否相等
    bool is_Equal(Fraction a) const;

    // 显示
    void disp();

    // 约分
    void redu();

    // 分数加(将当前对象的分子分母与参数分别相加，并覆盖当前对象)
    void add(Fraction a);

    // 分数减
    void sub(Fraction a);

    // 分数乘
    void mul(Fraction a);

    // 分数除
    void div(Fraction a);

    // 是否大于
    bool is_Greater(Fraction a) const;

    // 析构函数
    ~Fraction() {}

private:
    int nume, deno;
};

Fraction::Fraction() {
    setElem(0, 1);
}

void Fraction::setElem(int newNume, int newDeno) {
    try {
        nume = newNume;
        if (newDeno == 0) {
            deno = 1;
            throw IllegalException();
        } else {
            deno = newDeno;
        }
    } catch (IllegalException &e) {
        cout << "Illegal Denominator!" << endl;
    }
}

Fraction::Fraction(int newNume, int newDeno) {
    setElem(newNume, newDeno);
}

bool Fraction::is_Equal(Fraction a) const {
    if (deno == a.deno && nume == a.nume) return true;
    else return false;
}

void Fraction::disp() {
    Fraction::redu();
    if (nume == deno) {
        cout << 1 << endl;
    } else {
        cout << nume << "/" << deno << endl;
    }
}

void Fraction::redu() {
    int r, x = nume, y = deno;
    while (x != 0) {
        r = y % x;
        y = x;
        x = r;
    }
    nume /= y;
    deno /= y;
}

void Fraction::add(Fraction a) {
    nume = nume * a.deno + a.nume * deno;
    deno = deno * a.deno;
}

void Fraction::sub(Fraction a) {
    nume = nume * a.deno - a.nume * deno;
    deno = deno * a.deno;
}

void Fraction::mul(Fraction a) {
    nume = nume * a.nume;
    deno = deno * a.deno;
}

void Fraction::div(Fraction a) {
    try {
        if (a.nume == 0) {
            throw IllegalException();
        } else {
            nume = nume * a.deno;
            deno = deno * a.nume;
        }
    } catch (IllegalException &e) {
        cout << "Numerator cannot be 0!" << endl;
    }
}

bool Fraction::is_Greater(Fraction a) const {
    if (nume * a.deno > a.nume * deno) return true;
    else return false;
}

int main() {
    Fraction fraction1(2, 5);
    Fraction fraction2(1, 6);
    fraction1.add(fraction2);
    fraction1.disp();
    fraction1.setElem(2, 5);
    fraction1.sub(fraction2);
    fraction1.disp();
    fraction1.setElem(2, 5);
    fraction1.mul(fraction2);
    fraction1.disp();
    fraction1.setElem(2, 5);
    fraction1.div(fraction2);
    fraction1.disp();
    cout << fraction1.is_Greater(fraction2) << endl;
    cout << fraction1.is_Equal(fraction2) << endl;
    fraction1.setElem(2, 6);
    fraction1.disp();
    return 0;
}